package table_data;

import beans.YearBookPriority;
import common.Utils;
import java.io.File;
import java.io.FileOutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class YearBookPriorityCodeKbseToLocal {
    public static void main(String[] args) {
        try {
            File file=new File("C:\\Users\\ts647\\Desktop\\output_year_book_priotiry1");//文件不存在则创建
            FileOutputStream fos=new FileOutputStream(file,true);//第二个参数为true代表追加，为false(默认)为覆盖
            Class.forName(Utils.driver);//加载KBase驱动
            Connection conn = DriverManager.getConnection(Utils.yearbook_priority_url, Utils.user, Utils. password);// 获取连接
            Statement statement = conn.createStatement();//得到声明
            //当kbase某列值为null（看起来就是空）时，该列也存在，例子："unit_type_code":""
            ResultSet rs = statement.executeQuery("SELECT * FROM TJNJZKJB_NEW");//执行SQL语句返回结果集
            while (rs.next()) { //循环输出结果集
                String nian_jian_bian_ma=rs.getString("编号");
                String nian_jian_name=rs.getString("年鉴中文名");
                String nian_jian_nian_fen=rs.getString("年鉴年份");
                String guo_jia=rs.getString("国家");
                String sheng_fen=rs.getString("省");
                String shi=rs.getString("市");
                String xian=rs.getString("县");
                String new_year=rs.getString("最新年份");
                YearBookPriority ybp= new YearBookPriority(nian_jian_bian_ma,nian_jian_name,nian_jian_nian_fen,guo_jia,sheng_fen,shi,xian,new_year);
                fos.write(ybp.toString().getBytes());
            }
            fos.close();
            rs.close();//关闭结果集
            conn.close();//关闭数据库连接
        } catch (Exception e) {
            System.out.print("KBase ERROR:" + e.getMessage());
        }
    }
}

